<?php
/* @var $this UserServiceController */
/* @var $model UserService */
/* @var $form CActiveForm */
$user_id = Yii::app()->request->getParam(user_id);
$level=yii::app()->user->level;
$model2=UserService::model();
	if(!$user_id>0){
		$user_id=$model->user_id;
	}
Yii::app()->clientScript->registerScript('admin', "
$(document).ready(function(){
	var level = $('#level').val();
	$('#UserService_package_id').empty();
	$('label:eq(2)').hide();
	$('label:eq(3)').hide();
	$('#UserService_package_id').hide();
	$('#UserService_equipment_id').empty();
	$('#UserService_equipment_id').hide();
	if(level==1){
		$('#UserService_service_id').append('<option value=0>'+'请您选择'+'</option>');
		$('#UserService_service_id option[value=0] ').attr('selected',true);
		}
	$('#UserService_service_id').change(function(){
		//获取id的值
		var service_options = $(this).val();
		//alert(service_options);
		$('#UserService_package_id').empty();
		$('#UserService_package_id').hide();
		$('#UserService_package_id').append('<option value=0>'+'请您选择'+'</option>');
		$('#UserService_package_id option[value=0] ').attr('selected',true);
		$('#UserService_equipment_id').empty();
		$('#UserService_equipment_id').hide();
		$('#UserService_equipment_id').append('<option value=0>'+'请您选择'+'</option>');
		$('#UserService_equipment_id option[value=0] ').attr('selected',true);
		$('label:eq(2)').hide();
		$('label:eq(3)').hide();
		//根据id的值去判断b数组的匹配项,生成设备和套餐的下拉列表
		for(var i=0;i<b.length;i++){
			//alert(b[i][1]);
				if(service_options==b[i][1]){
					var option = $('<option>').text(b[i][2]).val(b[i][0]);
					$('#UserService_package_id').append(option);
					$('label:eq(2)').show();
					$('#UserService_package_id').show();
				}
			}
			//alert(service_options);
			if(service_options!=14){
				for(var i=0;i<c.length;i++){
					//alert(c[i][1]);
					if(service_options==c[i][1]){
						var option = $('<option>').text(c[i][2]).val(c[i][0]);
						$('#UserService_equipment_id').append(option);
						$('label:eq(3)').show();
						$('#UserService_equipment_id').show();
					}
				}
			}else if(service_options==14){
				//判断是否已有IPTV,如果有默认就是之前选过的那个.
				for(var i=0;i<c.length;i++){
					if(service_options==c[i][1]){
						if($('#UserService_user_id').val()==c[i][3]){
							var option = $('<option>').text(c[i][2]).val(c[i][0]);
							$('#UserService_equipment_id').empty();
							$('#UserService_equipment_id').append(option);
							$('label:eq(3)').show();
							$('#UserService_equipment_id').show();
							return;	
						}
					}
				}
				for(var i=0;i<c.length;i++){
					if(service_options==c[i][1]){
							var option = $('<option>').text(c[i][2]).val(c[i][0]);
							$('#UserService_equipment_id').append(option);
							$('label:eq(3)').show();
							$('#UserService_equipment_id').show();
					}
				}
			}
		})
	})
")
?>
<script type="text/javascript">
	// b是装包含package的数组 c是包含equipment的数组
 	b = new Array(); 
 	c = new Array();
 	d= new Array();
	<?php 
	$sql2 = "select * from zy_package";
	$connection = Yii::app()->db;
	$command = $connection->createCommand($sql2);
	$results2 = $command->queryAll();
	for($i=0;$i<count($results2);$i++){
	?>
	b[<?php echo $i;?>]= new Array("<?php echo $results2[$i]['package_id'];?>","<?php echo $results2[$i]['service_id'];?>","<?php echo $results2[$i]['package_name']?>");
	<?php 
}
	?>
	<?php
	//查询出未使用的和自己在使用的设备.
	$equipment_sql = "select * from zy_equipment where updater=0 or updater=".$user_id;
	$connection = yii::app()->db;
	$command = $connection->createCommand($equipment_sql);
	$equipment_result = $command->queryAll();
	for($i=0;$i<count($equipment_result);$i++){?>
		c[<?php echo $i;?>] = new Array("<?php echo $equipment_result[$i]['equipment_id']?>","<?php echo $equipment_result[$i]['service_id']?>","<?php echo $equipment_result[$i]['equipment_number']?>","<?php echo $equipment_result[$i]['updater']?>");
	<?php } ?>

</script>
<?php 
	$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'user-service-grid',
	'dataProvider'=>$model2->search2($user_id),
	//'filter'=>$model,
	'itemsCssClass'=>'responsive table table-striped table-bordered',
	'pager'=>array('class'=>'CLinkPager','htmlOptions'=>array('class'=>'pagination pull-right')),
	'pagerCssClass'=>'pagination pull-right',
	'cssFile'=>false,
	'columns'=>array(
	//	array('name'=>'user_service_id','headerHtmlOptions'=>array('style'=>'width:15%')),
		array('name'=>'user_id','value'=>'User::model()->getUserName($data->user_id)','type'=>'raw','headerHtmlOptions'=>array('style'=>"width:10%;")),
		array('name'=>'service_id','value'=>'Service::model()->getServiceName($data->service_id)','headerHtmlOptions'=>array('style'=>"width:10%;")),
		array('name'=>'package_id','value'=>'Package::model()->find("package_id=".$data->package_id)->package_name','headerHtmlOptions'=>array('style'=>'width:10%')),
		array('name'=>'package_price','value'=>'Package::model()->find("package_id=".$data->package_id)->package_price','headerHtmlOptions'=>array('style'=>'width:10%')),
		array('name'=>'deposit','headerHtmlOptions'=>array('style'=>'width:10%')),
		array('name'=>'equipment_number','headerHtmlOptions'=>array('style'=>'width:10%')),		
		array(
			'class'=>'CButtonColumn',
			'header'=>Yii::t('base','Operate'),
			'updateButtonImageUrl'=>array('class'=>'gicon-edit'),
			'updateButtonOptions'=>array('class'=>'btn btn-small'),
			'deleteButtonImageUrl'=>array('class'=>'gicon-edit'),
			'deleteButtonOptions'=>array('class'=>'btn btn-inverse btn-small'),
			'template'=>'{create login list}{delete}{update}',

			'buttons'=>array(

			'create login list'=>array(
				'label'=>Yii::t('base','create login list'),
				'options'=>array('class'=>'btn btn-small'),
				'url'=>'Yii::app()->createUrl("loginList/create",array("user_service_id"=>$data->user_service_id))',
				'visible'=>'$data->service_id!=14'

		)),
			'headerHtmlOptions'=>array('style'=>'width:10%'),
		),
	),
)); ?>
	
<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'user-service-form',
	'enableAjaxValidation'=>false,
)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>

	<?php echo $form->errorSummary($model); ?>

	

	<div class="row">
		<input type="hidden" id="level" name="level" value="<?php echo $level;?>"/>
		
		<?php echo $form->labelEx($model,'user_id'); ?>
		<?php 
			if($user_id!=""){
				echo $form->dropDownList($model,'user_id',User::model()->getAllTopUser($user_id));
			}else{
				echo $form->dropDownList($model,'user_id',UserService::model()->getuser($model->user_id));
			} ?>
		<?php echo $form->error($model,'user_id'); ?>
	</div>

	<div class="row">
		<?php
		if($user_id!=""){
			$level=yii::app()->user->level;
			if($level==2){
				if(count(UserService::model()->getTopServiceName())>0){
				echo $form->labelEx($model,'service_id');
				echo $form->dropDownList($model,'service_id',UserService::model()->getTopServiceName());
				echo $form->error($model,'service_id');
				}else{?>
					<input id="UserService_service_id" name="UserService[service_id]" type="hidden"> 
					<div class="errorMessage">You don't order the Service.</div>
				<?php }
			}elseif($level==1){
				echo $form->labelEx($model,'service_id');
				echo $form->dropDownList($model,'service_id',Service::model()->getAllService());
				echo $form->error($model,'service_id');
			}
			}else{	
				echo $form->labelEx($model,'service_id');
				echo $form->dropDownList($model,'service_id',UserService::model()->getservice($model->service_id));
				echo $form->error($model,'service_id');
		} ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'package_id'); ?>
		<?php 
			echo $form->dropDownList($model,'package_id',UserService::model()->getPackageName(),array('label'=>'value 2')); ?>
		<?php echo $form->error($model,'package_id'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'equipment_id'); ?>
		<?php echo $form->dropDownList($model,'equipment_id',UserService::model()->getEquipmentName()); ?>
		<?php echo $form->error($model,'equipment_id'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model,'valid_date'); ?>
		<?php   
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
    'name'=>'UserService[valid_date]',
	'id'=>'UserService_valid_date',
	'value'=>$model->valid_date!="0000-00-00"?$model->valid_date:date("Y-m-d"),
    // additional javascript options for the date picker plugin
    'options'=>array(
        'showAnim'=>'fold',
		'dateFormat'=>'yy-mm-dd',
    ),
    'htmlOptions'=>array(
        'style'=>'height:30px;'
    ),
));
?>  
		<?php echo $form->error($model,'valid_date'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'status'); ?>
		<?php echo $form->dropDownList($model,'status',array(0=>'停用',1=>'正在使用')); ?>
		<?php echo $form->error($model,'status'); ?>
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->